﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSharp.Libs.WPF;
using CSharp.Libs.Misc;
using System.Windows.Threading;
using System.Windows.Input;

namespace CSharp.Libs.WPF.UserControls
{
    class MessageLoggingTextBoxViewModel : ViewModelBase
    {
        #region declarations
        private Dispatcher _DISP = null;
        private MessageLogging _ML = null;

        private RelayCommand _rcClearLog = null;

        private string _strResult = "";
        private string _strCustomFormat = "<ID> - <Type> - <Occurred> - <Text>";

        private bool _fMessageFormatDefault = true;
        private bool _fMessageFormatCustom = false;

        private const string PLACEHOLDER_MESSAGE_ID = "<ID>";
        private const string PLACEHOLDER_MESSAGE_TYPE = "<Type>";
        private const string PLACEHOLDER_MESSAGE_OCCURRED = "<Occurred>";
        private const string PLACEHOLDER_MESSAGE_TEXT = "<Text>";
        #endregion



        #region constructor
        public MessageLoggingTextBoxViewModel(
            Dispatcher DISP)
        {
            _DISP = DISP;
        }
        #endregion



        #region private functions
        private string _getMessageText(
            MessageItem MI)
        {
            if (_fMessageFormatDefault)
            {
                return MI.ToString();
            }

            if (_fMessageFormatCustom)
            {
                return _strCustomFormat.Replace(PLACEHOLDER_MESSAGE_ID, MI.Id.ToString()).Replace(PLACEHOLDER_MESSAGE_TYPE, MI.Type.ToString()).Replace(PLACEHOLDER_MESSAGE_OCCURRED, MI.Occured.ToString()).Replace(PLACEHOLDER_MESSAGE_TEXT, MI.Text);
            }

            return "ERROR in _getMessageText";
        }

        private void _refreshMessages()
        {
            _DISP.Invoke((Action)delegate
            {
                _strResult = "";
                foreach (KeyValuePair<int, MessageItem> KVP in _ML.Messages)
                {
                    _strResult = string.Concat(_strResult, _getMessageText(KVP.Value), Environment.NewLine);
                }
                OnPropertyChanged("Result");
            });
        }

        private void _clearLog()
        {
            _DISP.Invoke((Action)delegate
            {
                _strResult = "";
                _ML.Messages.Clear();
                OnPropertyChanged("Result");
            });
        }
        #endregion



        #region event handling
        void _ML_AfterAddingMessage(object sender, MessageLoggingEventArgs mlea)
        {
            _DISP.Invoke((Action)delegate
            {
                _strResult = string.Concat(_strResult, _getMessageText(mlea.Message), Environment.NewLine);
                OnPropertyChanged("Result");
                OnPropertyChanged("CanClearLog");
            });
        }

        void _ML_AfterDeletingMessage(object sender, MessageLoggingEventArgs mlea)
        {
            _DISP.Invoke((Action)delegate
            {
                string _strRemove = string.Concat(_getMessageText(mlea.Message), Environment.NewLine);
                _strResult = _strResult.Replace(_strRemove, "");
                OnPropertyChanged("Result");
                OnPropertyChanged("CanClearLog");
            });
        }
        #endregion



        #region interface
        #region properties
        public MessageLogging MessageLogger
        {
            get { return _ML; }
        }

        public string Result
        {
            get { return _strResult; }
        }

        public string CustomFormatString
        {
            get { return _strCustomFormat; }
            set { _strCustomFormat = value; _refreshMessages(); }
        }

        public bool FormatDefault
        {
            get { return _fMessageFormatDefault; }
            set { _fMessageFormatDefault = value; _refreshMessages(); }
        }

        public bool FormatCustom
        {
            get { return _fMessageFormatCustom; }
            set { _fMessageFormatCustom = value; _refreshMessages(); }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <history>created  06.06.2012</history>
        public ICommand ClearLog
        {
            get
            {
                if (_rcClearLog == null)
                {
                    _rcClearLog = new RelayCommand(
                        param => this._clearLog(),
                        param => this.CanClearLog);
                }
                return _rcClearLog;
            }
        }
        public bool CanClearLog { get { return _strResult != ""; } }
        #endregion

        #region methods
        public void InitViewModel(
            MessageLogging ML)
        {
            _ML = ML;

            _ML.AfterAddingMessage += new MessageLogging.AfterAddingMessageEventHandler(_ML_AfterAddingMessage);
            _ML.AfterDeletingMessage += new MessageLogging.AfterDeletingMessageEventHandler(_ML_AfterDeletingMessage);
        }
        #endregion
        #endregion
    }
}
